import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Welcome from '../views/Welcome.vue'
//用户列表页面
import UserList from '../components/user/user_list.vue'

Vue.use(Router)
const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  { 
    path: '/login',
    component: Login
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
    redirect:'/welcom',
    children:[
      {
        path: '/welcom',
        component: Welcome
      },
      {
        path: '/users',
        component: UserList
      }
    ]
  }
]

const router = new Router({
  routes
})
// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!token) return next('/login')
  next()
})
export default router
